const Express = require('express');
const router = Express.Router();
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

const Food = require('../models/food');

// router.get('/:foodid',function (req, res, next) {
//     const foodid = req.params.foodid;
//     console.log(foodid);
//     res.send(foodid);
// });

router.get('/:foodname', async function (req, res, next) {
    const foodname = req.params.foodname;
    try {
        const foodSearchResults = await  Food.findAll({
            where: {
                name: {
                    [Op.like]: `%${foodname}%`
                }
            }
        });
        res.json(foodSearchResults.map(food => food.dataValues));
    } catch (e) {
        res.json({
            error: {
                message: 'mysql query error'
            }
        });
        throw e;
    }
});


module.exports = router;